package com.spring.authManage.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spring.authManage.dao.RoleInfoDao;
import com.spring.authManage.pojo.RoleInfo;
import com.spring.authManage.pojo.RoleInfoParam;
import com.spring.authManage.service.RoleInfoService;
import com.spring.common.util.ChunQiuTool;
import com.spring.security.CustomInvocationSecurityMetadataSourceService;

@Service("roleInfoService")
public class RoleInfoServiceImpl implements RoleInfoService{
	
	@Autowired
	private RoleInfoDao roleInfoDao;
	
	/**
	 * 根据查询条件获取角色信息 不分页
	 * @param param
	 * @return
	 */
	public List<RoleInfo> queryAllRoleInfoByParam(RoleInfoParam roleInfoParam) throws Exception {
		List<RoleInfo> roleInfoList = this.roleInfoDao.queryAllRoleInfoByParam(roleInfoParam);
		return roleInfoList;
	}
	/**
	 * 添加角色
	 * @param roleInfo
	 */
	public void addRoleInfo(RoleInfo roleInfo) throws Exception {
		this.roleInfoDao.addRoleInfo(roleInfo);
	}
	/**
	 * 根据角色ID获取角色信息
	 * @param roleId
	 */
	public RoleInfo getRoleInfoById(String roleId) throws Exception {
		return this.roleInfoDao.getRoleInfoById(roleId);
	}
	/**
	 * 更新角色信息
	 * @param info
	 */
	public void updateRoleInfo(RoleInfo roleInfo) throws Exception {
		this.roleInfoDao.updateRoleInfo(roleInfo);
	}
	/**
	 * 根据条件查询角色信息
	 * @param param
	 * @return
	 */
	public RoleInfo queryRoles(RoleInfo roleInfo) throws Exception {
		return this.roleInfoDao.queryRoles(roleInfo) ;
	}
	/**
	 * 更新角色权限分配
	 * 先删除后更新
	 * @param roleId
	 * @param resourceIds
	 */
	@Override
	@Transactional(rollbackFor = Exception.class, value = "transactionManager")
	public void udpateRoleResource(RoleInfo roleInfo) throws Exception {
		String role_id = roleInfo.getId();
		String operator = ChunQiuTool.getUserName();
		String[] arr_res_id = roleInfo.getResourceId().split(",");
		List<RoleInfo> roleResList = new ArrayList<RoleInfo>();
		for (int i = 0; i < arr_res_id.length; i++) {
			RoleInfo info = new RoleInfo();
			info.setId(role_id);
			info.setResourceId(arr_res_id[i]);
			info.setUpdateBy(operator);
			roleResList.add(info);
		}
		roleInfoDao.deleteRoleResources(role_id);
		roleInfoDao.updateRoleResources(roleResList);
		// 修改权限后刷新内存，重载资源权限map
		CustomInvocationSecurityMetadataSourceService.refresh();
	}
	/**
	 * 根据权限名称获取拥有该权限的所有角色信息
	 * @param resourceName
	 * @return
	 */
	public List<RoleInfo> getRoleInfosByResourceName(String resourceName) throws Exception {
		List<RoleInfo> roleInfoList = this.roleInfoDao.getRoleInfosByResourceName(resourceName);
		return roleInfoList;
	}
	/**
	 * 删除角色
	 * 
	 * @param role_id
	 */
	@Override
	@Transactional(rollbackFor = Exception.class, value = "transactionManager")
	public void deleteRoleById(String role_id) throws Exception {
		roleInfoDao.updateRoleById(role_id);
		roleInfoDao.deleteRoleResources(role_id);
	}
	/**
	 * 重启删除角色
	 * @param roleId
	 */
	public void restartRoleById(String roleId) throws Exception {
		this.roleInfoDao.restartRoleById(roleId);
	}
	/**
	 * 根据用户ID 获取该用户所有角色 
	 * @param userId
	 * @return
	 */
	public List<RoleInfo> getRoleInfosByUser(String userId) throws Exception {
		List<RoleInfo> roleInfoList = this.roleInfoDao.getRoleInfosByUser(userId);
		return roleInfoList;
	}
	/**
	 * 是否存在角色
	 * @param roleInfo
	 * @return
	 */
	public Integer isExistRole(RoleInfo roleInfo) throws Exception {
		return this.roleInfoDao.isExistRole(roleInfo);
	}
	/**
	 * 获取角色权限
	 * 
	 * @param request
	 * @param response
	 * @param roleId
	 */
	public List<RoleInfo> getRoleResource(String id) throws Exception{
		return roleInfoDao.getRoleResource(id);
	}
}
